package com.sdhs.projectschedule.service.impl;

import java.util.List;

import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.projectschedule.domain.GcProgressHandIn;
import com.sdhs.util.BidUtilization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdhs.projectschedule.mapper.GcProgressPaymentCollectionFillMapper;
import com.sdhs.projectschedule.domain.GcProgressPaymentCollectionFill;
import com.sdhs.projectschedule.service.IGcProgressPaymentCollectionFillService;

/**
 * 项目进度跟踪--回款跟踪子-回款填报Service业务层处理
 *
 * @author andong
 * @date 2025-03-18
 */
@Service
public class GcProgressPaymentCollectionFillServiceImpl implements IGcProgressPaymentCollectionFillService
{
    @Autowired
    private GcProgressPaymentCollectionFillMapper gcProgressPaymentCollectionFillMapper;

    @Autowired
    private IBusinessFileService businessFileService;
    /**
     * 查询项目进度跟踪--回款跟踪子-回款填报
     *
     * @param id 项目进度跟踪--回款跟踪子-回款填报主键
     * @return 项目进度跟踪--回款跟踪子-回款填报
     */
    @Override
    public GcProgressPaymentCollectionFill selectGcProgressPaymentCollectionFillById(Long id)
    {
        GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill = gcProgressPaymentCollectionFillMapper.selectGcProgressPaymentCollectionFillById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_PROGRESS_HAND_IN.getCode(), String.valueOf(id));
        gcProgressPaymentCollectionFill.setFileList(businessFiles);
        return gcProgressPaymentCollectionFill;
    }

    /**
     * 查询项目进度跟踪--回款跟踪子-回款填报列表
     *
     * @param gcProgressPaymentCollectionFill 项目进度跟踪--回款跟踪子-回款填报
     * @return 项目进度跟踪--回款跟踪子-回款填报
     */
    @Override
    public List<GcProgressPaymentCollectionFill> selectGcProgressPaymentCollectionFillList(GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill)
    {
        return gcProgressPaymentCollectionFillMapper.selectGcProgressPaymentCollectionFillList(gcProgressPaymentCollectionFill);
    }

    /**
     * 新增项目进度跟踪--回款跟踪子-回款填报
     *
     * @param gcProgressPaymentCollectionFill 项目进度跟踪--回款跟踪子-回款填报
     * @return 结果
     */
    @Override
    public int insertGcProgressPaymentCollectionFill(GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill)
    {
        int i = gcProgressPaymentCollectionFillMapper.insertGcProgressPaymentCollectionFill(gcProgressPaymentCollectionFill);
        handler(gcProgressPaymentCollectionFill.getId(), gcProgressPaymentCollectionFill);
        /*新增回款填报的时候计算我的项目-利润率测试-实施期利润率*/
        BidUtilization.UpdateBidUtilization(gcProgressPaymentCollectionFill.getProjectId());
        /*新增回款填报的时候修改我的项目-关联合同数据-累计值*/
        BidUtilization.UpdateProjectContract(gcProgressPaymentCollectionFill.getProjectId(), gcProgressPaymentCollectionFill.getContractId());
        return i;
    }

    /**
     * 修改项目进度跟踪--回款跟踪子-回款填报
     *
     * @param gcProgressPaymentCollectionFill 项目进度跟踪--回款跟踪子-回款填报
     * @return 结果
     */
    @Override
    public int updateGcProgressPaymentCollectionFill(GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill)
    {
        int i = gcProgressPaymentCollectionFillMapper.updateGcProgressPaymentCollectionFill(gcProgressPaymentCollectionFill);
        handler(gcProgressPaymentCollectionFill.getId(), gcProgressPaymentCollectionFill);
        return i;
    }

    /**
     * 批量删除项目进度跟踪--回款跟踪子-回款填报
     *
     * @param ids 需要删除的项目进度跟踪--回款跟踪子-回款填报主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressPaymentCollectionFillByIds(Long[] ids, String contractId)
    {
         Long projectId = 0L;
        if (ids != null && ids.length > 0) {
            Long firstId = ids[0]; // 获取第一个元素
            GcProgressPaymentCollectionFill  gcProgressPaymentCollectionFill = gcProgressPaymentCollectionFillMapper.selectGcProgressPaymentCollectionFillById(firstId);
            projectId = gcProgressPaymentCollectionFill.getProjectId();
        }
        int i = gcProgressPaymentCollectionFillMapper.deleteGcProgressPaymentCollectionFillByIds(ids);

        /*删除回款填报的时候计算我的项目-利润率测试-实施期利润率*/
        BidUtilization.UpdateBidUtilization(projectId);

        /*新增回款填报的时候修改我的项目-关联合同数据-累计值*/
        BidUtilization.UpdateProjectContract(projectId, contractId);
        return i;
    }

    /**
     * 删除项目进度跟踪--回款跟踪子-回款填报信息
     *
     * @param id 项目进度跟踪--回款跟踪子-回款填报主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressPaymentCollectionFillById(Long id)
    {
        return gcProgressPaymentCollectionFillMapper.deleteGcProgressPaymentCollectionFillById(id);
    }

    /**
     * 保存联合数据
     * 保存合同附件
     */
    public void handler(Long id, GcProgressPaymentCollectionFill gcProgressPaymentCollectionFill) {
        // 保存合同附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_PROGRESS_PAYMENT_COLLECTION_FILL.getCode(), String.valueOf(id), gcProgressPaymentCollectionFill.getFileList());
    }
}
